package org.ns.framework.webmvc;

import java.util.ArrayList;
import java.util.List;

import org.ns.framework.webmvc.error.ExceptionHandler;
import org.ns.framework.webmvc.interceptor.LocaleParameterInjector;
import org.ns.framework.webmvc.interceptor.LocaleParameterProperty;
import org.ns.framework.webmvc.interceptor.RequestInterceptor;
import org.ns.framework.webmvc.interceptor.TokenParameterInjector;
import org.ns.framework.webmvc.result.CustomResponseHandler;
import org.ns.framework.webmvc.result.JsonResponseHandler;
import org.ns.framework.webmvc.verify.LoginVerifyService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer, SmartInitializingSingleton, ApplicationContextAware {

    
	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
    // 请求拦截
    @Bean
    RequestInterceptor requestInterceptor() {
        return new RequestInterceptor();
    }
    // 登录验证
    @Bean
	LoginVerifyService loginVerifyService() {
        return new LoginVerifyService();
    }
    // json应答处理
    @Bean
	JsonResponseHandler jsonResponseHandler() {
        return new JsonResponseHandler();
    }
    @Bean
    CustomResponseHandler customResponseHandler () {
    	return new CustomResponseHandler();
    }
    // 错误拦截器
    @Bean
    ExceptionHandler exceptionHandler() {
        return new ExceptionHandler();
    }
    // token 参数注入
    @Bean
	TokenParameterInjector tokenParameterInjector() {
        return new TokenParameterInjector();
    }
    
    @Bean
    LocaleParameterInjector localeParameterInjector() {
    	return new LocaleParameterInjector();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor()).order(-1);
    }


    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "ns.webmvc.locale")
    @Bean
    LocaleParameterProperty localeParameterProperty() {
    	return new LocaleParameterProperty();
    };
    @Override
    public void afterSingletonsInstantiated() {
    	// TODO Auto-generated method stub
    	
//    }
//    @Override
//    public void afterPropertiesSet() {
    	// 请求映射处理适配器
    	RequestMappingHandlerAdapter requestMappingHandlerAdapter = applicationContext.getBean(RequestMappingHandlerAdapter.class);
    	// 参数处理
    	List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
    	List<HandlerMethodArgumentResolver> newArgumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
    	newArgumentResolvers.add(localeParameterInjector());
    	newArgumentResolvers.add(tokenParameterInjector());
    	newArgumentResolvers.addAll(argumentResolvers);
    	requestMappingHandlerAdapter.setArgumentResolvers(newArgumentResolvers);
    	// 结果处理
        List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> newReturnValueHandlers = new ArrayList<>();
        newReturnValueHandlers.add(customResponseHandler());
        newReturnValueHandlers.add(jsonResponseHandler());
        newReturnValueHandlers.addAll(returnValueHandlers);
        requestMappingHandlerAdapter.setReturnValueHandlers(newReturnValueHandlers);
    }
}
